package com.simple.space.config.shiro;

import com.simple.space.modules.shiro.mapper.UserMapper;
import com.simple.space.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName CustomRealm
 * @Author SimpleTree
 **/
@Component
public class CustomRealm extends AuthorizingRealm {

    private final UserMapper userMapper;

    @Autowired
    public CustomRealm(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /**
     * 此方法必须重写
     * @param token
     * @return
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }

    /**
     * 只有当需要检测用户权限的时候才会调用此方法，例如checkRole,checkPermission之类的
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("=====权限认证=====");
        String username = null;
        try {
            username = (String) JwtUtil.parseJWT(principalCollection.toString()).get("username");
        } catch (Exception e) {
            e.printStackTrace();
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获取该用户角色
        String role = userMapper.getRole(username);
        //每个用户有默认权限
        String rolePermission = userMapper.getRolePermission(username);
        //每个用户可以设置新权限
        String permission = userMapper.getPermission(username);
        Set<String> roleSet = new HashSet<>();
        Set<String> permissionSet = new HashSet<>();
        //需将role，permission封装Set作为info
        roleSet.add(role);
        permissionSet.add(rolePermission);
        permissionSet.add(permission);
        //设置该用户的角色和权限
        info.setRoles(roleSet);
        info.setStringPermissions(permissionSet);

        return info;
    }

    /**
     * 默认使用此方法进行用户名验证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("=====身份认证方法======");
        String token = (String) authenticationToken.getCredentials();

        Claims claims = null;
        try {
            claims = JwtUtil.parseJWT(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String username = (String) claims.get("username");

        if (username == null) {
            throw new AuthenticationException("token认证失败");
        }

        String password = userMapper.getPassword(username);
        if (password == null) {
            throw new AuthenticationException("该用户不存在");
        }
        int ban = userMapper.checkUserBanStatus(username);
        if (ban == 1) {
            throw new AuthenticationException("该用户已被封号");
        }
        return new SimpleAuthenticationInfo(token, token, "simple");
    }
}
